package screen;

import java.util.Vector;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.game.Sprite;

import animation.FlyText;

import main.GameCanvas;
import main.NuoiThuMidlet;
import model.Animal;
import model.IAction;
import model.PlayerInfo;
import model.Position;
import service.GlobalService;
import utilities.CRes;
import utilities.GameResource;
import utilities.TileMap;
import font.BitmapFont;

public class StudyEnglishScr extends PlayGameScr {
	static StudyEnglishScr instance;
	boolean m_bIsHaveChat = false;
	int yGameCanvas;
	String m_sLogChatTeacher;
	
	public static void clean(){
		if(instance != null){
			instance.m_imgBackground = null;
			instance.m_arrChatContent = null;
			instance.m_arrPlayers = null;
			instance.m_arrPos = null;
		}
		instance = null;		
	}
	
    public static StudyEnglishScr gI() {
        if (instance == null) {
            instance = new StudyEnglishScr(4);
        }
        return instance;
    }
    
	public StudyEnglishScr(int maxPlayer) {
		super(maxPlayer);
		
		// TODO Auto-generated constructor stub
		
		m_bIsDisplayAnimal = true;
		
		m_arrPos[0] = new Position(30, GameCanvas.h - 120);
		m_arrPos[0].anchor = 0;
		
		m_arrPos[1] = new Position(GameCanvas.w - 50, m_arrPos[0].y);
		m_arrPos[1].anchor = 1;
		
		m_arrPos[3] = new Position(m_arrPos[1].x, GameCanvas.h - 60);
		m_arrPos[3].anchor = 1;
		
		m_arrPos[2] = new Position(m_arrPos[0].x, m_arrPos[3].y);
		m_arrPos[2].anchor = 0;
		
		yGameCanvas = GameCanvas.h;
		
		Graphics g = m_imgBackground.getGraphics();
		g.setColor(0xc0e9e4);
		g.fillRect(0, 0, GameCanvas.w, GameCanvas.h);
		
		int topTmp = GameCanvas.h - 8 * 24 - 12;
		g.setColor(0x64a4d0);
		g.fillRect(0, topTmp, GameCanvas.w, 50);
		
		g.setColor(0x4d8a8a);
		g.fillRect(GameCanvas.hw / 2, topTmp - 60, GameCanvas.hw, 50);
		
		g.setColor(0x3d5e4e);
		g.fillRect(GameCanvas.hw / 2 + 5, topTmp - 55, GameCanvas.hw - 10, 40);
		
		BitmapFont.drawBoldFont(g, "TIÊN HỌC LỄ, HẬU HỌC VĂN", GameCanvas.hw, topTmp - 80, 0x495554, Graphics.HCENTER);
		
		int row, col;
		for(row = 1; row <= 8; row++){
			for(col = 0; col < GameCanvas.w; col += 24){
				TileMap.m_frameImg.drawFrame(24, col, GameCanvas.h - row * 24, Sprite.TRANS_NONE, g);
			}
		}
		
		//draw teacher's desk
		g.drawImage(GameResource.instance.imgTableTeacher, 2, GameCanvas.h - 8 * 24 - 18, 0);
		
		//draw but giang
		int widthButGiang = GameCanvas.w - GameResource.instance.imgTableTeacher.getWidth() * 2 - 4;
		int tmpX = GameResource.instance.imgTableTeacher.getWidth() + 6;
		int tmpY = GameCanvas.h - 8 * 24 - 18;
		g.setColor(0x9a4263);
		g.drawLine(tmpX, tmpY, tmpX, tmpY + GameResource.instance.imgButGiang.getHeight());
		g.setColor(0xc6996c);
		g.drawLine(tmpX, tmpY + GameResource.instance.imgButGiang.getHeight() - 1, tmpX, tmpY + GameResource.instance.imgButGiang.getHeight());
		tmpX++;
		
		for(col = 0; col < widthButGiang; col += GameResource.instance.imgButGiang.getWidth())
		{
			g.drawImage(GameResource.instance.imgButGiang, tmpX + col, tmpY, 0);
		}
		
		g.drawLine(tmpX + col, tmpY, tmpX + col, tmpY + GameResource.instance.imgButGiang.getHeight());
		
		
		m_iWMove = GameCanvas.w - GameResource.instance.imgTableTeacher.getWidth() * 2 - 40;
		m_iXMove = m_iWMove;
	}

	public void switchToMe(byte roomID1, byte boardID, Vector vtPlayers,
			int ownerID, int money) {
		// TODO Auto-generated method stub
		super.switchToMe(roomID1, boardID, vtPlayers, ownerID, money);
		
		int i;
		for(i = 0; i < vtPlayers.size(); i++){
			PlayerInfo p = (PlayerInfo)vtPlayers.elementAt(i);
			if(p!= null && p.m_iId != -1){
				p.m_defaultPet.m_position = m_arrPos[i];
			}
		}
	}

	public void setPlayer(PlayerInfo p, int seat) {
		// TODO Auto-generated method stub
		if(seat >=0 && seat < m_iMaxPlayer){
			m_arrPlayers[seat] = p;
			m_arrPlayers[seat].m_defaultPet.m_position = m_arrPos[seat];
		}
		
		p.isReady = false;
		p.isOwner = false;
	}
	
	public void onStartGame(int whoPlayFirst, short interval) {
		// TODO Auto-generated method stub
		GameCanvas.endDlg();
		m_bIsHaveChat = false;
		m_iSpeed = -1;
		m_cmdCenter = null;
		startGame();
	}
	
	public void paintBackground(Graphics g){
		g.translate(-g.getTranslateX(), -g.getTranslateY());
		g.setClip(0, 0, GameCanvas.w, GameCanvas.h);
		
		g.setColor(0xc0e9e4);
		g.fillRect(0, 0, GameCanvas.w, GameCanvas.h);
		
		int topTmp = GameCanvas.h - 8 * 24 - 12;
		g.setColor(0x64a4d0);
		g.fillRect(0, topTmp, GameCanvas.w, 50);
		
		g.setColor(0x4d8a8a);
		g.fillRect(GameCanvas.hw / 2, topTmp - 60, GameCanvas.hw, 50);
		
		g.setColor(0x3d5e4e);
		g.fillRect(GameCanvas.hw / 2 + 5, topTmp - 55, GameCanvas.hw - 10, 40);
		
		BitmapFont.drawBoldFont(g, "TIÊN HỌC LỄ, HẬU HỌC VĂN", GameCanvas.hw, topTmp - 80, 0x495554, Graphics.HCENTER);
		
		int row, col;
		for(row = 1; row <= 8; row++){
			for(col = 0; col < GameCanvas.w; col += 24){
				TileMap.m_frameImg.drawFrame(24, col, GameCanvas.h - row * 24, Sprite.TRANS_NONE, g);
			}
		}
		
		//draw teacher's desk
		g.drawImage(GameResource.instance.imgTableTeacher, 2, GameCanvas.h - 8 * 24 - 18, 0);
		
		//draw but giang
		int widthButGiang = GameCanvas.w - GameResource.instance.imgTableTeacher.getWidth() * 2 - 4;
		int tmpX = GameResource.instance.imgTableTeacher.getWidth() + 6;
		int tmpY = GameCanvas.h - 8 * 24 - 18;
		g.setColor(0x9a4263);
		g.drawLine(tmpX, tmpY, tmpX, tmpY + GameResource.instance.imgButGiang.getHeight());
		g.setColor(0xc6996c);
		g.drawLine(tmpX, tmpY + GameResource.instance.imgButGiang.getHeight() - 1, tmpX, tmpY + GameResource.instance.imgButGiang.getHeight());
		tmpX++;
		
		for(col = 0; col < widthButGiang; col += GameResource.instance.imgButGiang.getWidth())
		{
			g.drawImage(GameResource.instance.imgButGiang, tmpX + col, tmpY, 0);
		}
		//col -= GameResource.instance.imgButGiang.getWidth();
		g.drawLine(tmpX + col, tmpY, tmpX + col, tmpY + GameResource.instance.imgButGiang.getHeight());
		
	}
	
	public void paint(Graphics g){		
		g.translate(-g.getTranslateX(), -g.getTranslateY());
		g.setClip(0, 0, GameCanvas.w, GameCanvas.h);
		g.drawImage(m_imgBackground, 0, 0, 0);
		 		
		//paintBackground(g);
		
		int i, count = m_arrPlayers.length;
				
		for(i = 0; i < count; i++){
			//draw tables
			g.setColor(0x75b3e4);
			g.fillRect((i % 2 == 0)? m_arrPos[i].x: m_arrPos[i].x - 10, m_arrPos[i].y - 10, 17, 27);
			g.drawImage(GameResource.instance.imgTableStudent, (i % 2 == 0)? m_arrPos[i].x + 15: m_arrPos[i].x - 45, m_arrPos[i].y - 15, 0);

			PlayerInfo p = m_arrPlayers[i];
			if(p!= null && p.m_iId != -1){
				BitmapFont.drawOutlinedString(g, p.m_strName, m_arrPos[i].x, m_arrPos[i].y - p.m_defaultPet.animalInfo.m_iH[p.m_defaultPet.m_iAnimalStatus] - 15, 0xffffff, 0x000000, Graphics.HCENTER | Graphics.VCENTER);
				p.m_defaultPet.paintAction(g, p.m_defaultPet.m_iAnimalStatus, Animal.STAND, m_arrPos[i].x + ((m_arrPos[i].anchor == 1)?5:0), m_arrPos[i].y, m_arrPos[i].anchor);
				if(p.m_defaultPet.m_bEnableChat)
					p.m_defaultPet.paintBoxChatAnimals(g);
			}
		}
		
		//paint top bar
		PaintPopup.paintTopBar(g);
		
		//paint icon game
		GameResource.instance.m_frameIconItems.drawFrame(RoomListScr.m_iIconGame, 2, -1, 0, g);
		
		//paint board info
		BitmapFont.drawBoldFont(g, "Trường " + m_iRoomID + " - Lớp " + m_iBoardID, 30, 10, 0xffffff, Graphics.LEFT | Graphics.VCENTER);
		
		
		if (m_nMsg > 0) {
            BitmapFont.drawOutlinedString(g, m_nMsg + " ", GameCanvas.w - 20, 25, 0xffff00, 0x000000, Graphics.RIGHT | Graphics.VCENTER);
            g.drawImage(GameResource.instance.imgMesssage, GameCanvas.w - 20, 20, 0);
        }
		
		g.translate(-g.getTranslateX(), -g.getTranslateY());
		g.setClip(0, 0, GameCanvas.w, GameCanvas.h);
		
		if(GameCanvas.currentDialog != null && GameCanvas.currentDialog instanceof InputDlg)
			return;
		
		//paint marquee message
		if(m_strMarqueeMsg != null){
			paintMarqueeMsg(g);
			g.setClip(0, 0, GameCanvas.w, GameCanvas.h);
		}
		if (ChatTextField.gI().isShow) {
			PaintPopup.paintCmdBar(g, ChatTextField.gI().left, ChatTextField.gI().center, ChatTextField.gI().right);
		} else {
			if (!GameCanvas.menu.m_showMenu)
				PaintPopup.paintCmdBar(g, m_cmdLeft, m_cmdCenter, m_cmdRight);
		}
		
		int tmpX = GameResource.instance.imgTableTeacher.getWidth() + 20;
		int tmpY = yGameCanvas - 8 * 24;
		PaintPopup.paintTeacher(g, tmpX + m_iXMove, tmpY, m_iStatus, m_irotate);
		if(m_bIsHaveChat){
			paintChatTeacher(g, tmpX + m_iXMove, tmpY);
		}
	}
	
	private void paintChatTeacher(Graphics g, int x, int y){
		if(m_sLogChatTeacher == null)
			m_sLogChatTeacher = "";
		
		int topTmp = yGameCanvas - 8 * 24 - 12;
		String[] tmpString = BitmapFont.m_bmFont.splitFontBStrInLine(m_sLogChatTeacher, GameCanvas.hw - 18);
		int HString = BitmapFont.m_bmFont.getHeight()*(tmpString.length - 1);
		PaintPopup.paintBox(g, GameCanvas.hw / 2 +7, topTmp-40-HString, GameCanvas.hw - 14, 15+HString, 1, 0xffffff, 0x72beed);
		int tmpX = GameCanvas.hw / 2 + GameCanvas.hw - 17 ;
		if(x > tmpX){
			x = tmpX;
		}
		if(x < GameCanvas.hw / 2 +7)
			x = GameCanvas.hw / 2 +7;
		
		if(x < GameCanvas.hw)
			g.drawRegion(GameResource.instance.imgChatArrow, 0, 0, 10, 10, Sprite.TRANS_NONE, x, topTmp-26,  0);
		else
			g.drawRegion(GameResource.instance.imgChatArrow, 0, 0, 10, 10, Sprite.TRANS_MIRROR, x, topTmp-26,  0);
		
		int i;
		for(i = 0; i < tmpString.length ; i++ ){
			BitmapFont.drawNormalFont(g, tmpString[i], GameCanvas.hw / 2 + 10, topTmp-39 - HString + i*BitmapFont.m_bmFont.getHeight(), 0x000000, 0);
		}
	
	}
	
	
	int m_iWMove;
	int m_iSpeed = -1;
	int m_iXMove;
	int m_irotate = Sprite.TRANS_NONE;
	int m_iStatus = 0;
	int m_iTickChat = 200;
	int m_iTickGame = 200;
	
	
	public void update(){
		
		if(m_bIsHaveChat){
			
			m_iTickChat --;
			if(m_iTickChat < 0){
				m_iTickChat = CRes.random(200, 500);
				m_bIsHaveChat = false;
				m_iStatus = 4;
			}
			
			if(m_iTickChat%5 == 0){
				m_iStatus = (m_iStatus == 4)? 5 : 4;
			}
		}
		else{
			m_iXMove += m_iSpeed;
			if(m_iXMove < 0 || m_iXMove > m_iWMove){
				m_iSpeed *= -1;
				m_irotate = (m_irotate == Sprite.TRANS_NONE)? Sprite.TRANS_MIRROR : Sprite.TRANS_NONE;
			}
			m_iTickGame --;
			if(m_iTickGame%5 == 0){
				m_iStatus = (++m_iStatus)% 4;
			}
			if(m_iTickGame < 0){
				m_iTickGame = CRes.random(100, 300);
				m_bIsHaveChat = true;
			}
		}
		
		
		int i, count = m_arrPlayers.length;
		
		for(i = 0; i < count; i++){
			PlayerInfo p = m_arrPlayers[i];
			if(p!= null && p.m_iId != -1){
				p.m_defaultPet.updateChat();
			}
		}
		
		super.update();
	}
	
	public void onEndGame(int whoWin) {
		// TODO Auto-generated method stub
		PlayerInfo p = findPlayer(whoWin);
		GameCanvas.startOK(p.m_strName + " win", new IAction() {
			
			public void perform() {
				// TODO Auto-generated method stub
				stopGame();
				
				GameCanvas.endDlg();
			}
		});

		for(int i = 0; i < m_arrPlayers.length; i++){
			if(m_arrPlayers[i] != null && m_arrPlayers[i].m_iId != m_iOwnerID)
				m_arrPlayers[i].isReady = false;
		}		
	}
	
	public void showChat(int fromID, String text) {
		if(fromID == -99){
			//giao vien chat
		
			m_bIsHaveChat = true;
			m_sLogChatTeacher = text;
			
		}
		else{
			PlayerInfo p = findPlayer(fromID);
			if(p != null && p.m_iId != -1){
				p.m_defaultPet.chat(text);
			}
		}
		
	}
	
	public void onChatFromMe(String text) {
		if (text.trim().equals(""))
			return;
		GlobalService.gI().chatToTeacher(m_iRoomID, m_iBoardID, text);
		showChat(NuoiThuMidlet.m_myPlayerInfo.m_iId, text);
	}
	
	public void paintMarqueeMsg(Graphics g){
		PaintPopup.paintBox(g, 0, 0, GameCanvas.w, 20, 1, 0x000000, 0x000000);
    	BitmapFont.drawBoldFont(g, m_strMarqueeMsg, m_iPosXMsg, 10, 0xffffff, Graphics.LEFT | Graphics.VCENTER);	
	
		m_iTimeDisplay++;
		m_iPosXMsg-- ;
		if(m_iPosXMsg <= - m_iStrWidth){
			m_iPosXMsg = GameCanvas.w;
		}
		if(m_iTimeDisplay > 1000){
			m_iTimeDisplay = 0;
			m_strMarqueeMsg = null;
		}
	}
	
	public void setOwner(int iDNewOwner) {
		// TODO Auto-generated method stub
		m_iOwnerID = iDNewOwner;
		
		
		m_cmdLeft = menuNormalCmd;        	
		m_cmdCenter = null;
	}

	public void updateSmart(int whoWin, int smart) {
		// TODO Auto-generated method stub
		int pos = findPlayerPos(whoWin);
		if(pos != -1){

			FlyText text = new FlyText();
			text.startEffect(smart + "", m_arrPos[pos].x, m_arrPos[pos].y, 0x00ff00, 0x000000);	
		}
	}
}
